package DFS;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname 全排列46
 * @Description TODO
 * @Date 2023/3/6 21:01
 * @Created by xjl
 */
public class 全排列46 {

    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> lists=new ArrayList<>();
        if (nums.length==0){
            return lists;
        }
        List<Integer> list=new ArrayList<>();
        boolean[] visit=new boolean[nums.length];
        dfs(nums,0,visit,list,lists);
        return lists;
    }

    private void dfs(int[] nums, int index, boolean[] visit, List<Integer> list, List<List<Integer>> lists) {
        if (index==nums.length){
            lists.add(new ArrayList<Integer>(list));
            return;
        }
        for (int i=0;i<nums.length;i++){
            if (!visit[i]){
                visit[i]=true;
                list.add(nums[i]);
                dfs(nums,index+1,visit,list,lists);
                //回溯
                list.remove(list.size()-1);
                visit[i]=false;
            }
        }
    }
}
